/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MapEditor.java
 *
 * Created on 26.feb.2011, 13:00:26
 */
package mz2d.editor;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import mz2d.map.MapBuffer;
import mz2d.map.MapStreamer;

/**
 *
 * @author Laptop
 */
public class MapEditor extends javax.swing.JFrame {

    /** Creates new form MapEditor */
    public MapEditor() {
        initComponents();
        mapStreamer = new MapStreamer();
        mapImage = createImage(800, 640);
        try {
            tile0 = loadImageFile("./data/tile0.png");
            tile1 = loadImageFile("./data/tile1.png");
            tileLos = loadImageFile("./data/tileLos.png");
        } catch (IOException iex) {
            iex.printStackTrace();
        }
        renderMap();
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 798, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 576, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MapEditor().setVisible(true);
            }
        });


    }
    private MapStreamer mapStreamer;
    int xPos = 0;
    int yPos = 0;
    Image mapImage = null;
    Image tile0 = null;
    Image tile1 = null;
    Image tileLos = null;

    public void renderMap() {
        try {
            Graphics g = mapImage.getGraphics();
            MapBuffer mapBuffer = mapStreamer.getBuffer(xPos, yPos, 25, 20);
            for (int x = 0; x < 25; x++) {
                for (int y = 0; y < 20; y++) {
                    int tileType = mapBuffer.getTiles()[(mapBuffer.getTileWidth() * y) + x].getType();
                    if (tileType == 0) {
                        g.drawImage(tile0, x * 32, y * 32, null);
                    } else if (tileType == 1) {
                        g.drawImage(tile1, x * 32, y * 32, null);
                    } else if (tileType == -1) {
                        g.drawImage(tileLos, x * 32, y * 32, null);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Image loadImageFile(String loc) throws IOException {
        return ImageIO.read(new File(loc));
    }

    public void paint(Graphics g) {
        /**
         *
         */
        super.paint(g);

        /**
         * Draw map
         */
        if (mapImage != null) {
            g.drawImage(mapImage, 8, 24, null);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
